package com.xyy.saas.payment.util;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class MD5Util {

	public static String doEncrypt(Map<String, Object> map, String mchntKey) throws UnsupportedEncodingException {
		Object[] keys =  map.keySet().toArray();
		Arrays.sort(keys);
		StringBuilder originStr = new StringBuilder();
		for(Object key:keys){
			if(null!=map.get(key)&&!map.get(key).toString().equals("")&&!"signature".equals(key))
			originStr.append(key).append("=").append(map.get(key)).append("&");
		}
		originStr.append("key=").append(mchntKey);
		log.info("md5拼接字符串：{}", originStr.toString());
		String sign = DigestUtils.md5Hex(originStr.toString().getBytes("utf-8"));
		return sign;
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		HashMap hashMap = JSON.parseObject("{\"bankTransactionId\":\"4200000026201712218232795373\",\"amount\":380000,\"orderNo\":\"15138289703231067405\",\"signature\":\"908b410e632a8f51adf267c721f08884\",\"subject\":\"商城购物\",\"openId\":\"okX-h0e0Eyk7ZpRG4b_3ZvO1lNmQ\",\"fee\":2090,\"description\":\"\",\"body\":\"order\",\"paidTime\":\"20171221120656\",\"payPowerId\":\"8\",\"paySt\":2,\"orderDt\":\"20171221\",\"extra\":\"\",\"mchntId\":\"0000000861\",\"mchntOrderNo\":\"RN20171221120250289555\"}", HashMap.class);
		System.out.print(doEncrypt(hashMap,"0f7b977a73c9535a3a3c480b91682baa"));

	}
}
